package com.shakev.coupon.data.mysql.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shakev.coupon.business.util.JsonUtils;
import com.shakev.coupon.business.util.rest.RestResponse;
import com.shakev.coupon.data.mysql.entity.TkUser;
import com.shakev.coupon.data.mysql.service.ITkUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lws
 * @since 2019-06-11
 */
@RestController
@RequestMapping("/user")
public class TkUserController {

    @Resource
    ITkUserService userService;

    public static void main(String[] args) {
        TkUser user = new TkUser();
        user.setPhoneNum("123");
        user.setTaobaoAvaterUrl("123");
        user.setTaobaoNick("123");
        user.setTaobaoOpenId("123");
        user.setTaobaoOpenSid("123");

        System.out.println(JsonUtils.toJson(user));
    }

    @RequestMapping("/login")
    public RestResponse login(@RequestBody TkUser user){
        QueryWrapper<TkUser> queryWapper = new QueryWrapper<>();
        queryWapper.eq(true,"taobao_open_id",user.getTaobaoOpenId())
                .or(true).eq(true,"taobao_open_sid",user.getTaobaoOpenSid())
                .or(true).eq(true,"phone_num",user.getPhoneNum());
        TkUser data = userService.getOne(queryWapper);
        if(data==null){
            boolean res = userService.save(user);
            return RestResponse.success("goooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooood");
        }
            return RestResponse.success("goooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooood");


    }
}
